This invention relates generally to computer-implemented methods for optimizing instruction sequences involving creation, selection, use, and deletion of dynamically created computer resources. More specifically, the invention relates to methods for optimizing instruction files, known as journal files or metafiles, used in conjunction with Microsoft Windows(copyright) graphics display interfaces.
Graphical output under the Microsoft Windows 3.x(copyright) and Windows NT(copyright) operating systems is generally accomplished through calls to operating system functions. The operating system components used for carrying out graphics instructions are collectively referred to as the graphic device interface or GDI.
The Windows(copyright) GDI uses dynamically created objects or resources for carrying out drawing operations. Resources are classified into four different categories or classes: pens, brushes, palettes and fonts. One resource from each class can be currently selected at any given time. Different GDI resources are used to perform various types of drawing operations.
In general, resource-related GDI functions or instructions can be categorized as resource creation instructions, resource selection instructions, resource usage instructions, and resource deletion instructions. Before a resource is used it must be created with a resource creation instruction. It must then be selected using a resource selection instruction. Resource usage instructions are used to draw shapes with whatever resource has most recently been selected with a resource selection instruction. After use, resources are deleted with resource deletion instructions.
Resource usage instructions can be used to form a variety of shapes or images, such as lines, arcs, regions, ellipses, polygons, rectangles, and bitmaps. Drawing a straight line is an example of a simple operation that can be performed with GDI instructions. To draw a line, a pen is created, selected, used, and then deleted. This involves four different instructions or GDI function calls: CREATEPEN, SELECTOBJECT, LINETO, and DELETEOBJECT.
Some GDI instructions are dependent on previous instructions. For instance, resource selection instructions are dependent on previously issued resource creation instructionsxe2x80x94before a resource can be selected it must be created. Similarly, resource usage instructions are dependent on previously issued resource creation and selection instructionsxe2x80x94before a resource can be used it must be both created and selected.
Many GDI operations are also dependent on computer-maintained state information such as existing resources and their attributes, and graphics modes. State information includes such things as aspect ratio, pixel origin, background color, display mode, text color, text alignment, window origins, color adjustment, direction of polygon filling, view scaling, clipping, etc. In this discussion, state information also includes the identification of currently selected resources.
The combination of all current state information is referred to as a state context. The Windows(copyright) GDI maintains a context stack. The current state context can be pushed onto or popped from the stack. This provides an easy way for an application program to perform GDI operations without permanently changing the state context.
In addition to providing a convenient way to create device-independent graphics images, the Windows(copyright) GDI can be used to create data files for storing such images. Rather than storing the images in bit-mapped format, they are stored as an ordered sequence of GDI instructions which are xe2x80x9cplayedxe2x80x9d to create an actual display image. The Windows(copyright) GDI contains functions for recording and storing GDI instructions in such files, which are referred to in the Windows(copyright) environment as xe2x80x9cmetafiles.xe2x80x9d A metafile is simply a collection of GDI commands that creates desired text or images. Because metafiles take up less space and are more device-independent than bitmaps, they provide convenient storage for images that appear repeatedly in an application or need to be moved from one application to another.
To generate a metafile, a Windows(copyright) application creates a special device context that sends GDI commands to a metafile for storage. The application can later play back the metafile and display the image. During playback, the GDI breaks the metafile down into records, each containing an instruction, and identifies each created resource with an object index value that indexes a handle table. After a DELETEOBJECT instruction is encountered during playback, the associated object index value is re-used by the next resource that the metafile creates.
A metafile consists of two pertinent parts: a header and a list of records. The header contains a description of the size of the metafile, the number of drawing resources it uses, and other information. Each metafile record contains a header, a GDI instruction, and its arguments.
GDI functions and metafiles will be familiar to those accustomed to programming in the Windows(copyright) environment. For more information regarding these subjects, refer to the Windows 3.1(copyright) and Windows NT(copyright) Programing Manuals, available from Microsoft Corporation of Redmond, Wash. Both of these references are hereby incorporated by reference.
While metafiles offer an attractive alternative to bit-mapped files, further size reduction would be welcomedxe2x80x94particularly in the context of newer consumer-oriented applications which attempt to utilize graphical user interfaces to a degree that has previously not been attempted. The invention described below meets this need by reducing the number of records or instructions in metafiles. In addition, the methods described below result in optimized metafiles which can be played back more quickly than the original, non-optimized metafiles.
This invention is a method for optimizing metafiles. In the preferred embodiment, an optimization program reads instructions sequentially from an original metafile. The records are filtered and in some cases rearranged, and emitted to an optimized metafile. The optimized metafile, when played back, results in the same image as the original metafile. However, there are comparatively fewer records and instructions in the optimized metafile than in the original metafile.
The optimization program uses several methods to achieve optimization. One such method is to defer emission of any given instruction until such emission is actually necessary. For instance, a resource selection instruction that selects a particular resource is not emitted to the optimized metafile until the optimization program encounters a subsequent resource usage instruction in the original metafile that attempts to use the same resource. In many cases this results in indefinitely deferring the resource selection instruction, thereby reducing the number of emitted instructions. Another method is to skip or omit the emission of any state modification instruction (including resource selection instructions) that would not result in a change to current state information.
These situations happen more frequently than might be thought, because of the tendency for programs to perform drawing operations using self-contained sequences of instructions. For example, a particular application program function might be responsible for drawing a circle at supplied coordinates. Because the function might be called from a multitude of other program locations, the function cannot make any assumptions about current state information or selected resources. Rather, the function must set any necessary state information upon to which drawing operations might be dependentxe2x80x94in spite of the fact that the appropriate state information might already be set.
Another method used in the preferred embodiment of the invention is to avoid destroying any resources until metafile playback is complete. Rather, resources are left in existence for subsequent use. Upon encountering a new resource creation instruction in the original metafile, the optimization program compares the characteristics of the new resource against characteristics of resources already in existence. If a match is found, a previously created resource is used and the new resource creation instruction is not emitted. This again results in fewer instruction emissions, thereby reducing the metafile size and speeding playback of the metafile.